import log4js from 'log4js'

log4js.configure({
  appenders: {
    access: {
      type: 'dateFile',
      filename: 'logs/access.log',
      category: 'http'
    },
    app: {
      type: 'dateFile',
      filename: 'logs/app.log',
      maxLogSize: 10485760,
      numBackups: 3
    },
    errorFile: {
      type: 'file',
      filename: 'logs/errors.log'
    },
    errors: {
      type: 'logLevelFilter',
      level: 'ERROR',
      appender: 'errorFile'
    }
  },
  // 对日志进行分类
  categories: {
    default: {
      appenders: ['app', 'errors'],
      level: 'INFO'
    },
    http: {
      appenders: ['access'],
      level: 'DEBUG'
    }
  }
})

export default (app) => {
  let httpLogger = log4js.getLogger('http')
  app.use(log4js.connectLogger(httpLogger, { level: 'auto' }))
}
